# Maintainer: Haroogan <Haroogan@gmail.com>

_realname=tor
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.4.8.4
pkgrel=1
pkgdesc="Anonymizing overlay network (mingw-w64)"
url="https://www.torproject.org/"
license=('spdx:BSD-3-Clause')
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
depends=("${MINGW_PACKAGE_PREFIX}-libevent"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-autotools"
  "${MINGW_PACKAGE_PREFIX}-ca-certificates")
source=("https://dist.torproject.org/${_realname}-${pkgver}.tar.gz"{,.sha256sum{,.asc}})
sha256sums=('09c1ce74a25fc3b48c81ff146cbd0dd538cbbb8fe4e2964fc2fb2b192f6a1d2b'
            'f4127d40d6301b3e949d1e37105eaa18a230848a6d8b0593489a320792eb6c86'
            'SKIP')
# list of keys https://gitlab.torproject.org/tpo/core/tor/-/raw/main/README.md
validpgpkeys=(
  '514102454D0A87DB0767A1EBBE6A0531C18A9179' # Alexander Færøy <ahf@0x90.dk>
  'B74417EDDF22AC9F9E90F49142E86A2A11F48D36' # David Goulet <dgoulet@ev0ke.net>
  '2133BC600AB133E1D826D173FE43009C4607B1FB' # Nick Mathewson <nickm@alum.mit.edu>
)

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  CPPFLAGS="-isystem ${MINGW_PREFIX}/include"
  CFLAGS="-pipe -O3 -fomit-frame-pointer -funroll-loops"
  LDFLAGS="-s -Wl,-s"
  "${srcdir}/${_realname}-${pkgver}/configure" \
    --prefix="${MINGW_PREFIX}" \
    --build="${MINGW_CHOST}" \
    --disable-seccomp \
    --disable-libscrypt \
    --enable-lzma \
    --enable-zstd

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  make test || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  make DESTDIR="${pkgdir}" install

  cd "${srcdir}/${_realname}-${pkgver}"

  install -D "LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  install -d           "${pkgdir}${MINGW_PREFIX}/var/lib/tor"
}
